<?php   // $Id: fact_edit.php 288 2008-09-20 09:35:54Z pcoustillas $ ?>
<?php
/*Copyright (C) 2003-2004 Guy Hendrickx
Licensed under the terms of the GNU  General Public License:http://www.opensource.org/licenses/gpl-license.php
File Authors:Guy Hendrickx 
Modification: Pierre Coustillas */
 
require_once("include/verif.php");
require_once("include/config/common.php");
require_once("include/config/var.php");
require_once("include/language/$lang.php");
require_once("include/utils.php");
require_once("include/headers.php");

$num_fact=isset($_GET['num_fact'])?$_GET['num_fact']:"";

function aff_detail_bon ($p_num_bon)
{
//cette fonction permet d'afficher dans un tableau html un bon de commande détaillé.
//cette fonction est appelée 2 fois dans cette page d'où le besoin de cette fonction
//cette fonction est appelée qd l'option detail_facture est à oui
	global $tblpref;

	?>
	<table width="100%">
		<tr>
			<th class="petit" width="10%"><?php aff_lang("Qte") ?></th>
			<th class="petit" width="10%"><?php aff_lang("Unité") ?></th>
			<th class="petit" width="10%"><?php aff_lang("P.U. HT") ?></th>
			<th class="petit" width="25%"><?php aff_lang("Article") ?></th>
			<th class="petit" width="15%"><?php aff_lang("Taux TVA") ?></th>
			<th class="petit" width="15%"><?php aff_lang("Prix HT") ?></th>
			<th class="petit" width="15%"><?php aff_lang("Prix TTC") ?></th>
		</tr>
		<?php
		$sql = "SELECT * FROM ${tblpref}cont_bon RIGHT JOIN ${tblpref}article ON 
				${tblpref}cont_bon.article_num = ${tblpref}article.num
				WHERE bon_num=$p_num_bon";
		$result =mysql_query($sql) or die("Erreur SQL !<br/>$sql<br/>" . mysql_error());

		while($data = mysql_fetch_array($result))
		{
			$quanti = $data['quanti'];
			$article = $data['article'];
			$tot_htva = $data['tot_art_htva'];
			$tot_tva = $data['to_tva_art'];
			$tx_tva = $data['taux_tva'];
			$prix_htva = $data['prix_htva'];
			$unite=$data['uni'];
			?>
			<tr>
				<td style="text-align:right;padding-right:5px"><?php echo $quanti ?> &nbsp;</td>
				<td style="text-align:left"> <?php echo $unite ?></td>
				<td style="text-align:right;padding-right:5px"><?php echo montant_financier($prix_htva) ?></td>
				<td style="text-align:center"><?php echo $article ?></td>
				<td style="text-align:right;padding-right:5px"><?php echo montant_financier($tx_tva) ?></td>
				<td style="text-align:right;padding-right:5px"><?php echo montant_financier($tot_htva) ?></td>
				<td style="text-align:right;padding-right:5px"><?php echo montant_financier($tot_tva + $tot_htva) ?></td>
			</tr>
			<?php
		}
		?>

	</table>

<?php

}

?>

<table width="760" border="0" class="page" align="center">
	<tr>
		<td class="page" align="center">
			<?php
			require_once("include/head.php");
			?>
		</td>
	</tr>
	<tr>
		<td class="page" align="center">
			<?php
			if ($user_fact == "n") { 
				echo "<h1>$lang_facture_droit</h1>";
				exit;
			}
			$sql = "SELECT valide,client,total_fact_h,total_fact_ttc,acompte,DATE_FORMAT(date_fact,'%d/%m/%Y') AS date_fact 
					FROM ${tblpref}facture WHERE num=$num_fact";
			$req = mysql_query($sql) or die("Erreur SQL sélection facture n°$num_fact !<br/>$sql<br/>" . mysql_error());
			$data = mysql_fetch_array($req);
			//$list_num = unserialize($data['list_num']);
			$num_client= $data['client'];
			$date_fact=$data['date_fact'];
			$tot_ht = $data['total_fact_h'];
			$tot_fact =  $data['total_fact_ttc'];
			$acompte = $data['acompte'];
			$tot_tva = $tot_fact - $tot_ht;
			$valide = $data['valide'];

			$sql = "SELECT nom FROM ${tblpref}client WHERE num_client=$num_client";
			$result=mysql_query($sql) or die("Erreur SQL !<br/>$sql<br/>" . mysql_error());
			$nom_client = mysql_result($result,0);
			 ?>
			<center>
			<?php 
			if ($valide==1)
			{
				echo "<h1>" . get_lang("Attention cette facture est validé ! Ce qui signifie que vous l'avez peut-être déjà envoyé au client") . "</h1>";
			}	
			?>
			<table width="70%">
				<caption><?php echo get_lang("Edition de la facture n° ") . $num_fact ?></caption>
				<tr>
					<td class="grand"><?php	aff_lang("Client"); ?></td>
					<td class="grand"><?php	echo $nom_client; ?></td>
					<td class="grand"><?php	aff_lang("Montant HT"); ?></td>
					<td class="grand" style="text-align:right"><?php echo avec_virgule($tot_ht) ?></td>
					<td>&nbsp;</td>
				</tr>

				<tr>
					<td class="grand"><?php	aff_lang("Date"); ?></td>
					<td class="grand"><?php	echo $date_fact; ?></td>
					<td class="grand"><?php	aff_lang("Montant TVA"); ?></td>
					<td class="grand" style="text-align:right"><?php echo avec_virgule($tot_tva) ?></td>
					<td>&nbsp;</td>
				</tr>

				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td class="grand"><?php	aff_lang("Accompte"); ?></td>
					<form action="fact_modif_acompte.php" method="post">
						<td class="grand" style="text-align:right">
							<input type="hidden" value="<?php echo $num_fact ?>" name="num_fact"/>
							<input type="hidden" value="<?php echo $tot_fact ?>" name="tot_fact"/>
							<input name="acompte" class="grand" type="text" style="text-align:right" value="<?php echo avec_virgule($acompte) ?>" size="8"/>
						</td>
						<td><input type="image" src="image/ok_kde.png" style="border: none; margin: 0;" alt="valider" /></td>
					</form>
				</tr>

				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td class="grand"><?php	aff_lang("Montant TTC"); ?></td>
					<td class="grand" style="text-align:right"><?php echo avec_virgule($tot_fact - $acompte) ?></td>
					<td>&nbsp;</td>
				</tr>

			</table>

			<br/>

			<table class="boiteaction">
				<caption><?php aff_lang("Bons de commandes présent dans la facture") ?></caption>
				<tr>
					<th width="20%">N° Bon</th>
					<th width="20%">Date</th>
					<th width="20%">Montant HT</th>
					<th width="20%">Montant TTC</th>
					<th width="20%">voir</th>
					<th width="20%">Retirer</th>
					

				</tr>
				<?php
				//afficher tout les bons de commandes pouvant être retirer de la factures

				$sql="SELECT DATE_FORMAT(date,'%d/%m/%Y') AS date, tot_htva, tot_tva, num_bon
						from ${tblpref}bon_comm WHERE num_facture=$num_fact";
				$result = mysql_query($sql) or die("Erreur SQL sur sélection des bon de la fact numero $num_fact !<br/>$sql<br/>" . mysql_error());

				while ($data = mysql_fetch_array($result))
				{
					//$data = mysql_fetch_array($req);
					$date = $data['date'];
					$tot_htva =$data['tot_htva'];
					$tot_tva =$data['tot_tva'];
					$num_bon = $data['num_bon'];
					$ttc =($tot_htva + $tot_tva);
					?>
				<tr>
					<td class='texte1'><?php echo "$num_bon";?></td>
					<td class='texte1'><?php echo "$date" ?></td>
					<td class='texte1' style="text-align:right;padding-right:5px"><?php echo "$tot_htva $devise" ?></td>
					<td class='texte1' style="text-align:right;padding-right:5px"><?php echo "$ttc $devise" ?></td>
					<td class='texte1'>
						<form action="fpdf/bon_pdf.php" method="post" enctype="multipart/form-data" target="_blank">
							<input type="hidden" name="num_bon" value="<?php   echo $num_bon; ?>" />
							<input type="hidden" name="user" value="adm" />
							<input type="image" src="image/imprimante.png" alt="imprimer" />
						</form>
					</td>
					<td class='<?php echo couleur_alternee (FALSE); ?>'>
						<form action="fact_add_remove_bon.php" method="post" onClick='return confirmDelete()'>
							<input type="hidden" name="retirer" value="<?php echo "$num_bon"; ?>" />
							<input type="hidden" name="num_fact" value="<?php echo "$num_fact"; ?>" />
							<input type="submit" value="retirer" />
						</form>
					</td>
				</tr>
					<?php
					if (option("facture->detail_facture")=="oui")
					{
					?>
					<tr>
						<td style="vertical-align:middle;text-align:center">Détail</td>
						<td colspan="4">
							<?php aff_detail_bon($num_bon); ?>
						</td>
					</tr>
					<tr><td colspan="6">&nbsp;</td></tr>
					<?php
					}
				}
				?>
			</table>

			<br/>
			<hr/>
			<br/>
			<?php	//afficher tout les bons de commandes pouvant être ajouter à la factures ?>


			<table class="boiteaction">
				<caption><?php aff_lang("Bons de commandes pouvant être ajouté à la facture") ?></caption>
				<tr>
					<th width="20%">N° Bon</th>
					<th width="20%">Date</th>
					<th width="20%">Montant HT</th>
					<th width="20%">Montant TTC</th>
					<th width="20%">voir</th>
					<th width="20%">Ajouter</th>
				</tr>
				<?php
				$sql = "SELECT num_bon, tot_htva, tot_tva, nom, DATE_FORMAT(date,'%d/%m/%Y') AS date 
					FROM ${tblpref}bon_comm 
					RIGHT JOIN ${tblpref}client on ${tblpref}bon_comm.client_num = num_client 
					WHERE num_facture=0 AND client_num = '$num_client'
					ORDER BY ${tblpref}bon_comm.`num_bon` DESC";
		
				$req = mysql_query($sql) or die('Erreur SQL !<br>'.$sql.'<br>'.mysql_error());

				while($data = mysql_fetch_array($req))
				{
					$aj_num_bon = $data['num_bon'];
					$date =$data['date'];
					$tot_htva =$data['tot_htva'];
					$tot_tva =$data['tot_tva'];
					$ttc =($tot_htva + $tot_tva);
				?>
				<tr>
					<td class='<?php echo couleur_alternee (); ?>'><?php echo "$aj_num_bon"; ?></td>
					<td class='<?php echo couleur_alternee (FALSE); ?>'><?php echo "$date "; ?></td>
					<td class='<?php echo couleur_alternee (FALSE); ?>' style="text-align:right;padding-right:5px"><?php echo "$tot_htva $devise" ?></td>
					<td class='<?php echo couleur_alternee (FALSE); ?>' style="text-align:right;padding-right:5px"><?php echo "$ttc $devise"; ?></td>
					<td class='<?php echo couleur_alternee (FALSE); ?>'>
						<form action="fpdf/bon_pdf.php" method="post" enctype="multipart/form-data" target="_blank">
							<input type="hidden" name="num_bon" value="<?php echo $aj_num_bon; ?>" />
							<input type="hidden" name="user" value="adm" />
							<input type="image" src="image/imprimante.png" alt="imprimer" />
						</form>
					</td>

					<td class='<?php   echo couleur_alternee (FALSE); ?>'>
						<form action="fact_add_remove_bon.php" method="post" onClick='return confirmDelete2()' />
							<input type="hidden" name="ajouter" value="<?php echo "$aj_num_bon"; ?>" onselect="submit" />
							<input type="hidden" name="num_fact" value="<?php echo "$num_fact"; ?>" />
							<input type="submit" value="ajouter" />
						</form>
					</td>

				</tr>
					<?php
					if (option("facture->detail_facture")=="oui")
					{
					?>
					<tr>
						<td style="vertical-align:middle;text-align:center">Détail</td>
						<td colspan="4">
							<?php aff_detail_bon($aj_num_bon); ?>
						</td>
					</tr>
					<tr><td colspan="6">&nbsp;</td></tr>
					<?php
					}
				}
				?>
				<tr>
					<td class="submit" colspan="5">&nbsp;</td>
				</tr>
			</table>
			</center>
		</td>
	</tr>
	<tr>
		<td>
			 <?php
			 require_once("include/bas.php");
			 ?>
		 </td>
	</tr>
</table>

</body>
</html>
